<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>

    <h1>拦截练习</h1>
    <input type="text">
    <button>提交</button>

    <script>
        const oIpt = document.querySelector("input")
        const oBtn = document.querySelector("button")
        //创建一个axios实例
        const myAxios = axios.create({
            baseURL: "/",
            timeout: 10000,
            headers: {}
        })
        // 给axios实例设置请求拦截，并将token添加到请求头文件（config.headers）
        myAxios.interceptors.request.use(config => {
            const token = "hrsfijseawd,awdawdawdwa,gsedgsef"
            config.headers.token = token
            return config
        })

        oBtn.onclick = async function () {
            let iptData = oIpt.value //获取输入框输入的值，提交以后对这个值做判断
            try {
                const re = await myAxios.get("/user-practice", {
                    params: {
                        userId: iptData
                    }
                })
                console.log(re);
            } catch (e) {
                console.log(e);
            }
        }
    </script>
</body>

</html>